<?php

namespace app\model;

use app\Request;
use think\facade\Db;

/**
 * 采购订单
 */
class XlPurchaseOrderOutModel
{
    public static function signfor($data, $id)
    {
        return Db::table('xl_purchase_orderout')->where('id', '=', $id)->update($data);
    }

    public static function list_bac($param): array
    {
        $userIds = getScopeView($param['uid']);
        if ($userIds != 0) {
            $where[] = ['order.uid', 'in', $userIds];
        }
        $where[] = [
            'order.partner_id', '=', $param['partner_id']
        ];
        $where[] = ['order.status', '=', 1];
        if ($param['id'] > 0) {
            $orderInfo = Db::table('xl_purchase_order')->where('id', $param['id'])->find();
            if (strpos($orderInfo['out_order_ids'], ',') !== false) {
                $orderOutIds = explode(",", $orderInfo['out_order_ids']);
            } else {
                $orderOutIds = [$orderInfo['out_order_ids']];
            }
            $where[] = ['order.id', 'in', $orderOutIds];
        }
        if ($param['type'] > 0) {
            $where[] = ['order.type', '=', $param['type']];
        }
        if ($param['review_status'] != 0) {
            if ($param['review_status'] == -1) {
                $where[] = ['order.review_status', 'in', [0, 1]];
            } else {
                $where[] = ['order.review_status', '=', $param['review_status']];
            }
        }
        if ($param['order_status'] != 0) {
            $where[] = ['order.order_status', '=', $param['order_status']];
        }
        if ($param['otype'] > 0) {
            $where[] = ['order.otype', '=', $param['otype']];
        }
        if (!empty($param['name'])) {
            $where[] = ['order.name', 'like', '%' . $param['name'] . '%'];
        }
        if (!empty($param['no'])) {
            $where[] = ['order.no', 'like', $param['no']];
        }
        if (!empty($param['stime']) && !empty($param['etime'])) {
            if ($param['stime'] && $param['etime']) {
                $where[] = ['order.add_time', 'between', [$param['stime'], $param['etime']]];
            }
        }
        if ($param['is_export'] == 1) {
            $param['limit'] = Db::table('xl_purchase_orderout')->alias('order')->where([$where])->count();
        }
        $list = Db::table('xl_purchase_orderout')->alias('order')
            ->join('xl_customer customer', 'customer.id=order.customer_id', 'left')
            ->join('xl_networks network', 'network.id=order.network_id', 'left')
            ->join('xl_supplier suppliers', 'suppliers.id=order.supplier_id', 'left')
            ->where([$where])->field('order.id,order.no,order.supplier_id,order.order_money,order.review_status,order.settlement_status,order.estimate_send_time,
            order.customer_id,order.mk_uid,order.ex_uid,order.add_time,order.review_time,order.remarks,order.order_status,order.goods_count as all_quantity,order.cate_count,
            order.document_time,order.sign_money as all_save_price,order.sign_count as all_save_count,customer.name as customer_name,order.network_id,network.name as network_name ,
            suppliers.id as supplier_id,suppliers.name as supplier_name')
            ->order("id desc")
            ->limit(($param['page'] - 1) * $param['limit'], $param['limit'])
            ->select()->toArray();
        foreach ($list as $k => $v) {
            $paidMoney = Db::table('xl_payment_note_log')->where('order_id', $v['id'])->sum('pay_money');
            $list[$k]['paid'] = $paidMoney;
            $list[$k]['mk_man'] = Db::table('xl_partner_user')->where('id', $v['mk_uid'])->value('name');
            $list[$k]['ex_man'] = Db::table('xl_partner_user')->where('id', $v['ex_uid'])->value('name');
            $checkHasGetIn = Db::table('xl_purchase_order')->where('out_order_id', $v['id'])->find();
            if (!empty($checkHasGetIn)) {
                $list[$k]['warehousing_time'] = $checkHasGetIn['sign_time'];
                $list[$k]['out_time'] = $checkHasGetIn['out_time'];
            } else {
                $list[$k]['warehousing_time'] = "";
                $list[$k]['out_time'] = "";
            }
            $orderStatus = self::getStatus($checkHasGetIn, $param, $v);
            $list[$k]['approval_status'] = $v['order_status'];
            $list[$k]['order_status'] = $orderStatus;
        }
        $count = Db::table('xl_purchase_orderout')->alias('order')->where($where)->count();
        return ['list' => $list, 'count' => $count];
    }

    public static function list($param): array
    {
        $userIds = getScopeView($param['uid']);
        if ($userIds != 0) {
            $where[] = ['order.uid', 'in', $userIds];
        }
        $where[] = [
            'order.partner_id', '=', $param['partner_id']
        ];
        $where[] = ['order.status', '=', 1];
        if (isset($param['id']) && ($param['id'] > 0)) {
            $orderInfo = Db::table('xl_purchase_order')->where('id', $param['id'])->find();
            if (strpos($orderInfo['out_order_ids'], ',') !== false) {
                $orderOutIds = explode(",", $orderInfo['out_order_ids']);
            } else {
                $orderOutIds = [$orderInfo['out_order_ids']];
            }
            $where[] = ['order.id', 'in', $orderOutIds];
        }
        if (isset($param['type']) && ($param['type'] > 0)) {
            $where[] = ['order.type', '=', $param['type']];
        }
        if (isset($param['review_status']) && ($param['review_status'] != 0)) {
            if ($param['review_status'] == -1) {
                $where[] = ['order.review_status', 'in', [0, 1]];
            } else {
                $where[] = ['order.review_status', '=', $param['review_status']];
            }
        }
        if (isset($param['order_status']) && ($param['order_status'] != 0)) {
            $where[] = ['order.order_status', '=', $param['order_status']];
        }
        if (isset($param['otype']) && ($param['otype'] > 0)) {
            $where[] = ['order.otype', '=', $param['otype']];
        }
        if (!empty($param['name'])) {
            $where[] = ['order.name', 'like', '%' . $param['name'] . '%'];
        }
        if (!empty($param['no'])) {
            $where[] = ['order.no', 'like', $param['no']];
        }
        if (!empty($param['stime']) && !empty($param['etime'])) {
            if ($param['stime'] && $param['etime']) {
                $where[] = ['order.add_time', 'between', [$param['stime'], $param['etime']]];
            }
        }

        if (isset($param['network_id']) && ($param['network_id'] > 0)) {
            $where[] = ['network.id', '=', $param['network_id']];
        }

        if (isset($param['is_export']) && ($param['is_export'] == 1)) {
            $param['limit'] = Db::table('xl_purchase_orderout')->alias('order')->where([$where])->count();
        }
        $list = Db::table('xl_purchase_orderout')->alias('order')
            ->join('xl_customer customer', 'customer.id=order.customer_id', 'left')
            ->join('xl_networks network', 'network.id=order.network_id', 'left')
            ->join('xl_supplier suppliers', 'suppliers.id=order.supplier_id', 'left')
            ->where([$where])->field('order.id,order.no,order.supplier_id,order.order_money,order.review_status,order.settlement_status,
            order.customer_id,order.mk_uid,order.ex_uid,order.add_time,order.review_time,order.remarks,order.order_status,order.cate_count,order.goods_count,order.tax_rate,
            order.document_time,customer.name as customer_name,order.network_id,network.name as network_name,suppliers.id as supplier_id,suppliers.name as supplier_name,order.estimate_send_time,order.driver_commission,order.company_commission,order.platform_commission,order.settlement_time,order.channel')
            ->order("id desc")
            ->limit(($param['page'] - 1) * $param['limit'], $param['limit'])
            ->select()->toArray();
        foreach ($list as $k => $v) {
            $checkHasGetIn = Db::table('xl_purchase_order')->where('out_order_id', $v['id'])->find();
            $list[$k]['approval_status'] = $v['order_status'];
            $list[$k]['order_status']= self::getStatus($checkHasGetIn,$param,$v);
            if ($v['channel'] == 1) {
                $list[$k]['channels'] = '管理员下单';
            }elseif ($v['channel'] == 2) {
                $list[$k]['channels'] = 'OMS下单';
            }else{
                $list[$k]['channels'] = '其他下单';
            }
            $list[$k]['network_level'] = 1;
            if (!empty($v['order_money'])) {
                $list[$k]['tax_money'] = bcsub($v['order_money'], bcmul($v['order_money'], $v['tax_rate'], 2), 2);
            } else {
                $list[$k]['tax_money'] = 0;
            }
            if (!empty($v['mk_uid'])) {
                $list[$k]['mk_man'] = Db::table('xl_partner_user')->where('id', $v['mk_uid'])->value('name');
            } else {
                $list[$k]['mk_man'] = '';
            }
            if (!empty($v['ex_uid'])) {
                $list[$k]['ex_man'] = Db::table('xl_partner_user')->where('id', $v['ex_uid'])->value('name');
            } else {
                $list[$k]['ex_man'] = '';
            }
        }
        $count = Db::table('xl_purchase_orderout')->alias('order')
            ->join('xl_customer customer', 'customer.id=order.customer_id', 'left')
            ->join('xl_networks network', 'network.id=order.network_id', 'left')
            ->join('xl_supplier suppliers', 'suppliers.id=order.supplier_id', 'left')
        ->where($where)->count();
        return ['list' => $list, 'count' => $count];
    }

    public static function add($data)
    {
        return Db::table('xl_purchase_orderout')->insertGetId($data);

    }

    public static function edit($data, $id)
    {
        return Db::table('xl_purchase_orderout')->where('id', '=', $id)->update($data);
    }

    public static function del($id)
    {
        return Db::table('xl_purchase_orderout')->where('id', '=', $id)->update(['status' => 3, 'update_time' => getNowDateTime()]);

    }

    public static function info($id)
    {
        $where[] = ['order.id', '=', $id];
        $info = Db::table('xl_purchase_orderout')->alias('order')
            ->join('xl_partner_user user', 'user.id=order.uid', 'left')
            ->join('xl_supplier suppliers', 'suppliers.id=order.supplier_id', 'left')
            ->join('xl_customer customer', 'customer.id=order.customer_id', 'left')
            ->join('xl_networks network', 'network.id=order.network_id', 'left')
            ->where([$where])->field('order.*,user.name as uname,customer.name as customer_name,network.name as network_name,suppliers.id as supplier_id,suppliers.name as supplier_name')
            ->find();
        $goods = XlPurchaseOrderGoodsOutModel::getAll($id);
        foreach ($goods as $k => $v) {
            $goods[$k]['list'] = XlPurchaseOrderDetailOutModel::getAll($v['id']);
        }
        $info['goods'] = $goods;
        $info['all_price'] = Db::table('xl_purchase_order_detailout')->where('order_id', '=', $id)->sum('is_pick_count*purchase_price');
        $info['all_cate'] = Db::table('xl_purchase_order_detailout')->where('order_id', '=', $id)->count();
        return $info;

    }

    public static function detail($id)
    {
        $where[] = ['order.id', '=', $id];
        return Db::table('xl_purchase_orderout')->alias('order')
            ->where([$where])->field('order.order_status, order.cate_count, order.goods_count')
            ->find();

    }

    public static function infoAll($ids)
    {
        return Db::table('xl_purchase_orderout')->where('id', 'in', $ids)->select()->toArray();
    }

    public static function getStatus($info, $param, $item)
    {
        if (isset($param['otype']) && ($param['otype'] == 1)) {
            if ($item['order_status'] == 1) {
                return "待批准";
            }
            if (empty($info)) {
                return "待签收";
            }
            if ($info['order_status'] >= 2) {
                return "已入库";
            } else {
                return "待签收";
            }
        } else {
            if ($item['order_status'] == 1) {
                return "待批准";
            }
            if (empty($info)) {
                return "待出库";
            };
            if ($info['order_status'] == 6) {
                return "已出库";
            } else {
                return "待出库";
            }
        }
    }

    public static function getOrderType($otype, $type)
    {
        if ($otype == 1 && $type == 1) {
            return "采购订单";
        } elseif ($otype == 1 && $type == 2) {
            return "消退订单";
        } elseif ($otype == 2 && $type == 1) {
            return "销售订单";
        } else {
            return "退货订单";
        }
    }
}
